<template> 
  <el-dialog
    :title="!admin.id ? '新增用户' : '修改用户'"
    :close-on-click-modal="true"
    :visible.sync="visible">
    <el-form :model="admin"
             :rules="rules"
             ref="admin"
             label-width="150px" size="small">
      <el-form-item label="帐号：" prop="username" required>
        <el-input v-model="admin.username" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="姓名：" >
        <el-input v-model="admin.nickName" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：">
        <el-input v-model="admin.email" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password" required>
        <el-input v-model="admin.password"  type="password" style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="备注：">
        <el-input v-model="admin.note"
                  type="textarea"
                  :rows="5"
                  style="width: 250px"></el-input>
      </el-form-item>
      <el-form-item label="是否启用：">
        <el-radio-group v-model="admin.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="onSubmit('admin')">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import {getAdmin,createAdmin,updateAdmin} from '@/api/ums/admin';

  const defaultAdmin = {
    id: '',
    username: '',
    password: '',
    nickName: '',
    email: '',
    note: '',
    status: 1
  };
  export default {
    name: 'adminDetail',
    data() {
      return {
        visible: false,
        admin: Object.assign({}, defaultAdmin),
        rules: {
          username: [
            {required: true, message: '请输入账号', trigger: 'blur'}
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        },
      }
    },
    created() {

    },
    methods: {
      //初始化
      init (id) {
        this.admin.id = id || 0;
        this.visible = true
        if (!this.admin.id) {
          this.resetForm('admin');
        } else {
          // 修改
          getAdmin(id).then(response => {
            if(response && response.code == 200){
              this.admin = response.data;
            }else{
              this.resetForm('admin');
            }
          });
        }
      },
      //提交
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交用户', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.admin.id) {
                updateAdmin(this.admin.id, this.admin).then(response => {
                  if(response && response.code == 200){
                    this.resetForm('admin');
                    this.$message({
                      message: '修改成功',
                      type: 'success',
                      duration:1000,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    });
                  }else {
                    this.$message.error(response.msg)
                  }
                });
              } else {
                createAdmin(this.admin).then(response => {
                  if(response && response.code == 200){
                    this.resetForm('admin');
                    this.$message({
                      message: '添加成功',
                      type: 'success',
                      duration:1000,
                      onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                      }
                    });
                  }else {
                    this.$message.error(response.msg)
                  }
                });
              }
            });
          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration:1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.admin = Object.assign({},defaultAdmin);
      }
    }
  }
</script>
<style>
</style>


